package com.ruoyi.base.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.base.domain.BaseClass;
import com.ruoyi.base.service.IBaseClassService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级Controller
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Api("班级信息管理")
@RestController
@RequestMapping("/base/clazz")
public class BaseClassController extends BaseController
{
    @Autowired
    private IBaseClassService baseClassService;

    /**
     * 查询班级列表
     */
    @ApiOperation("获取班级列表")
    @PreAuthorize("@ss.hasPermi('base:clazz:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseClass baseClass)
    {
        startPage();
        List<BaseClass> list = baseClassService.selectBaseClassList(baseClass);
        return getDataTable(list);
    }
    

    /**
     * 查询班级列表
     */
    @ApiOperation("获取班级列表")
    @GetMapping("/select")
    public TableDataInfo select(BaseClass baseClass)
    {
        List<BaseClass> list = baseClassService.selectBaseClassList(baseClass);
        return getDataTable(list);
    }
    
    /**
     * 查询班主任的班级列表
     */
    @ApiOperation("获取班主任的班级列表")
    @GetMapping("/master")
    public TableDataInfo master(BaseClass baseClass)
    {        
        baseClass.setMasterUser(SecurityUtils.getLoginUser().getUser().getUserId());
        List<BaseClass> list = baseClassService.selectBaseClassList(baseClass);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @ApiOperation("导出班级列表")
    @PreAuthorize("@ss.hasPermi('base:clazz:export')")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BaseClass baseClass)
    {
        List<BaseClass> list = baseClassService.selectBaseClassList(baseClass);
        ExcelUtil<BaseClass> util = new ExcelUtil<BaseClass>(BaseClass.class);
        return util.exportExcel(list, "班级数据");
    }

    /**
     * 获取班级详细信息
     */
    @ApiOperation("获取班级详细信息")
    @ApiImplicitParam(name = "id", value = "班级ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:clazz:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(baseClassService.selectBaseClassById(id));
    }

    /**
     * 新增班级
     */
    @ApiOperation("新增班级")
    @PreAuthorize("@ss.hasPermi('base:clazz:add')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseClass baseClass)
    {
        return toAjax(baseClassService.insertBaseClass(baseClass));
    }

    /**
     * 修改班级
     */
    @ApiOperation("修改班级")
    @PreAuthorize("@ss.hasPermi('base:clazz:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseClass baseClass)
    {
        return toAjax(baseClassService.updateBaseClass(baseClass));
    }

    /**
     * 删除班级
     */
    @ApiOperation("删除班级")
    @ApiImplicitParam(name = "id", value = "班级ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:clazz:remove')")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseClassService.deleteBaseClassByIds(ids));
    }
    
    /**
     * 禁用班级
     */
    @ApiOperation("禁用班级")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:course:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
	@GetMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable Long[] ids)
    {
        return toAjax(baseClassService.updateBaseClassStatus(ids, "0"));
    }
    
    /**
     * 启用班级
     */
    @ApiOperation("启用班级")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:course:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
	@GetMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable Long[] ids)
    {
        return toAjax(baseClassService.updateBaseClassStatus(ids, "1"));
    }
    
    /**
     * 设置班级为毕业状态
     */
    @ApiOperation("设置班级为毕业状态")
    @ApiImplicitParam(name = "id", value = "课程ID", required = true, dataType = "int", paramType = "path")
    @PreAuthorize("@ss.hasPermi('base:course:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
	@GetMapping("/graduation/{ids}")
    public AjaxResult graduation(@PathVariable Long[] ids)
    {
        return toAjax(baseClassService.graduationClass(ids));
    }
    
    @Log(title = "导入班级信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BaseClass> util = new ExcelUtil<BaseClass>(BaseClass.class);
        List<BaseClass> teacherList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = baseClassService.importClass(teacherList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<BaseClass> util = new ExcelUtil<BaseClass>(BaseClass.class);
        return util.importTemplateExcel("班级数据");
    }
}
